package com.edu.utils;

import com.alibaba.druid.pool.DruidDataSource;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JdbcUtils {

    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;

    static {
        try {
            //读取db.properties文件中的数据库连接信息
            InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties prop = new Properties();
            prop.load(in);
            //获取数据库连接驱动
            driver = prop.getProperty("jdbc.driverClassName");
            //获取数据库连接URL地址
            url = prop.getProperty("jdbc.url");
            //获取数据库连接用户名
            username = prop.getProperty("jdbc.username");
            //获取数据库连接密码
            password = prop.getProperty("jdbc.password");
        } catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    /**
     * 获取德鲁伊数据源
     * @return DataSource
     */
    public static DataSource getDataSource(){
        DruidDataSource ds=new DruidDataSource();
        //设置驱动类全称
        ds.setDriverClassName(driver);
        //设置连接的数据库
        ds.setUrl(url);
        //设置用户名
        ds.setUsername(username);
        //设置密码
        ds.setPassword(password);
        //设置最大连接数量
        // ds.setMaxActive(Integer.parseInt(maxActive));
        return ds;
    }

    //加载数据库驱动
    //Class.forName(driver);

    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
    /**
     * 获取数据库连接
     * @return Connection
     * @throws SQLException
     */

    public static Connection getConnection(){
        try {
            Connection conn = threadLocal.get();
            if (conn == null) {
                //加载数据库驱动
                Class.forName(driver);
                conn = DriverManager.getConnection(url, username, password);
                threadLocal.set(conn);
            }
            return conn;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 关闭数据库连接
     */
    public static void closeConnection() {
        Connection conn = threadLocal.get();
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                threadLocal.remove();
            }
        }
    }

    /**
     * 开启事务
     */
    public static void startTransaction() {
        try {
            Connection conn = threadLocal.get();
            if (conn == null) {
                conn = getConnection();
                threadLocal.set(conn);
            }
            conn.setAutoCommit(false);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void commit() {
        try {
            Connection conn = threadLocal.get();
            if (conn != null) {
                conn.commit();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void rollback() {
        try {
            Connection conn = threadLocal.get();
            if (conn != null) {
                conn.rollback();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 执行增删改
     * @param sql
     * @param parms
     * @return
     */
    public static int executeUpdate(String sql, Object... parms) {
        PreparedStatement ps = null;
        Connection connection = threadLocal.get();
        try {
            ps = connection.prepareStatement(sql);
            for (int i = 0; i < parms.length; i++) {
                ps.setObject(i + 1, parms[i]);
            }
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
